package com.offices.subscribe.controller;


import com.alibaba.fastjson.JSONObject;
import com.offices.subscribe.dto.AccessToken;
import com.offices.subscribe.services.MenuAddService;
import com.offices.subscribe.utils.HttpRequestMethod;
import com.offices.subscribe.dto.tabmenu.BaseButton;
import com.offices.subscribe.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    @Autowired
    AccessToken accessToken;

    @RequestMapping(value = "/addmenu", method = RequestMethod.GET)
    public String addMenuButton() throws IOException {
//        声明请求参数map集合
        Map<String, List> paramsMap = new HashMap<>();
//        添加菜单按钮
        List<BaseButton> menuButtons = MenuAddService.addButton();
        //将按钮添加进请求参数
        paramsMap.put("button", menuButtons);
        JSONObject jsonObject = HttpClientUtil
                .doRequest("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken.getAccess_token(),
                        HttpRequestMethod.POST,
                        paramsMap
                );

        if (jsonObject.getString("errcode") == "40018") {
            return jsonObject.getString("errmsg");
        }
        return jsonObject.getString("errcode");
        //return JSON.toJSONString(paramsMap);

    }


}
